package cn.kgc.vuedemoebook.controller;

import cn.kgc.vuedemoebook.pojo.Entry;
import cn.kgc.vuedemoebook.pojo.ResultEntity;
import cn.kgc.vuedemoebook.service.EntryServcie;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@RestController
public class EntryController {


    @Resource
    EntryServcie entryServcie;


    //根据分类id查询
    @RequestMapping("/selBycategoryId")
    public ResultEntity<PageInfo<Entry>> selBycategoryId(@RequestParam(value = "pageNum",defaultValue = "1",required = false)Integer pageNum,
                                                         @RequestParam(value = "pageSize",defaultValue = "5",required = false)Integer pageSize,

                                                         @RequestParam(value = "categoryId",defaultValue = "0",required = false)Integer categoryId){
        PageInfo<Entry> pageInfo = entryServcie.selAllBycategoryId(pageNum, pageSize, categoryId);


        return ResultEntity.successWithoutData(pageInfo);
    }

    //增加或保存 无id的实体类为增加 有id的实体类为修改
    @RequestMapping("/saveEntry")
    public ResultEntity<String> saveEntry(@RequestBody Entry entry){
        Integer i;
        try {
            if(entry.getId()==null){
                i=entryServcie.add(entry);
            }else {
                i=entryServcie.update(entry);
            }
            return ResultEntity.successWithoutData(i+"");
        } catch (Exception e) {
            return ResultEntity.failed(e.getMessage());
        }



    }
    //根据id查询书
    @RequestMapping("/selById")
    public ResultEntity<Entry> selById(Integer id){
        Entry entry = entryServcie.selById(id);

        return ResultEntity.successWithoutData(entry);
    }

    //根据id 删除
        @RequestMapping("/delById")
    public ResultEntity<String> delById(Integer id){

        try {
            int del = entryServcie.del(id);
            return ResultEntity.successWithoutData(del+"");
        } catch (Exception e) {
           return ResultEntity.failed(e.getMessage());
        }
    }



}
